package com.tuantuan.schoolhelper.activity;

import io.rong.imkit.RongIM;
import io.rong.imkit.widget.provider.InputProvider;
import io.rong.imlib.model.Conversation;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.tuantuan.schoolhelper.R;
import com.tuantuan.schoolhelper.bean.StudentBean;
import com.tuantuan.schoolhelper.utils.Constant;

public class SearchActivity extends Activity implements
		SearchView.OnQueryTextListener {

	protected static final String TAG = SearchActivity.class.getSimpleName();
	ListView listView;
	SearchView searchView;
	Object[] names;
	ArrayList<String> mAllList = new ArrayList<String>();
	ArrayList<String> showAllList = new ArrayList<String>();

	private List<StudentBean> mStudentList;

	Myadapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		initActionbar();
		listView = (ListView) findViewById(R.id.list);
		if (adapter == null) {
			adapter = new Myadapter();
		}
		
		searchView.setOnQueryTextListener(this);
		searchView.setSubmitButtonEnabled(false);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				StudentBean bean=mStudentList.get(position);
				InputProvider.ExtendProvider[] provider = {};
				RongIM.getInstance().resetInputExtensionProvider(Conversation.ConversationType.PRIVATE, provider);
				RongIM.getInstance().startPrivateChat(SearchActivity.this, bean.getStuno(), bean.getStuName());

			}
		});

	}

	public void initActionbar() {
		// 自定义标题栏
		getActionBar().setDisplayShowHomeEnabled(false);
		getActionBar().setDisplayShowTitleEnabled(false);
		getActionBar().setDisplayShowCustomEnabled(true);
		LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View mTitleView = mInflater.inflate(R.layout.custom_action_bar_layout,
				null);
		getActionBar().setCustomView(
				mTitleView,
				new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT,
						LayoutParams.WRAP_CONTENT));

		searchView = (SearchView) mTitleView.findViewById(R.id.search_view);
	}

	@Override
	public boolean onQueryTextChange(String newText) {
		if (!TextUtils.isEmpty(newText)) {
			searchItem(newText);
		} else {
			mStudentList.clear();
		}
		updateLayout();
		return false;
	}

	@Override
	public boolean onQueryTextSubmit(String query) {
		// TODO Auto-generated method stub
		System.out.println("回车");

		return false;
	}

	/**
	 * 查询数据
	 * 
	 * @param name
	 */
	public void searchItem(String name) {
		// ArrayList<String> mSearchList = new ArrayList<String>();

		// TODO
		HttpUtils utils = new HttpUtils();
		RequestParams params = new RequestParams();
		// 添加参数
		params.addBodyParameter("name", name);
		utils.send(HttpMethod.POST, Constant.GETSTUDENTBYNAME, params,
				new RequestCallBack<String>() {
					@Override
					public void onFailure(HttpException ex, String msg) {
						ex.printStackTrace();
						Log.d(TAG, "连接失败！" + msg);

					}

					@Override
					public void onSuccess(ResponseInfo<String> info) {
						String json = info.result;
						setData(json);
					}

				});
	}

	private void setData(String json) {
		 
		mStudentList=new ArrayList<StudentBean>();
		StudentBean bean=null;
		try {
			JSONArray jar=new JSONArray(json);
			for(int i=0;i<jar.length();i++){
				JSONObject obj=(JSONObject) jar.get(i);
					bean=new StudentBean();
					bean.setStuId(obj.getInt("stuId"));
					bean.setStuName(obj.getString("stuname"));
					bean.setStuno(obj.getString("stuno"));
					mStudentList.add(bean);
					}
			updateLayout();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
					
						
			
	}

	/**
	 * 更新ui
	 */
	public void updateLayout() {
		listView.setAdapter(adapter);
	}

	public class Myadapter extends BaseAdapter {

		@Override
		public int getCount() {
			if (mStudentList!=null) {
				return mStudentList.size();
			}else{
				return 0;
			}
		}

		@Override
		public Object getItem(int position) {
			if (mStudentList!=null) {
				return mStudentList.get(position);
			}else{
				return null;
			}
		}

		@Override
		public long getItemId(int position) {
			if (mStudentList!=null) {
				return position;
			}else{
				return 0;
			}
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView==null){
				convertView=View.inflate(SearchActivity.this, R.layout.list_item, null);
			}
			TextView text=(TextView) convertView.findViewById(R.id.text1);
			text.setText(mStudentList.get(position).getStuName());
			return convertView;
		}


	}

}
